package com.yiyi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yiyi.entity.Choice;
import com.yiyi.form.SearchQuestionForm;
import com.yiyi.mapper.ChoiceMapper;
import com.yiyi.service.ChoiceService;
import com.yiyi.form.AddChoiceForm;
import com.yiyi.vo.ChoicePageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yiyi
 * @date 2021/5/30
 */
@Service
public class ChoiceServiceImpl implements ChoiceService {

    @Autowired
    private ChoiceMapper choiceMapper;

    /**
     * 保存新题
     *
     * @param addChoiceForm
     * @return
     * */
    @Override
    public int save(AddChoiceForm addChoiceForm) {
        Choice choice = new Choice();
        // 使用当前时间戳为其id
        choice.setChoiceId(String.valueOf(System.currentTimeMillis()));
        choice.setChoiceGrade(addChoiceForm.getChoiceGrade());
        choice.setChoiceTitle(addChoiceForm.getChoiceTitle());
        choice.setOptionA(addChoiceForm.getOptionA());
        choice.setOptionB(addChoiceForm.getOptionB());
        choice.setOptionC(addChoiceForm.getOptionC());
        choice.setOptionD(addChoiceForm.getOptionD());
        choice.setChoiceKey(addChoiceForm.getChoiceKey());
        // 将列表转化为字符串进行保存
        choice.setChoiceTag(addChoiceForm.getChoiceTag().toString());
        // ****************************************************************************
        // TODO 需调用information微服务获取该员工对应的公司id并保存至其中
        // ****************************************************************************
        choice.setChoiceCompanyId("1234567891234");
        return choiceMapper.insert(choice);
    }

    /**
     * 根据ID删除题目
     *
     * @param id
     * @return
     * */
    @Override
    public int delete(String id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("choice_id", id);
        return choiceMapper.delete(queryWrapper);
    }

    /**
     * 分页搜索及查询
     *
     * @param searchQuestionForm
     * @return
     * */
    @Override
    public ChoicePageVO findByPage(SearchQuestionForm searchQuestionForm) {
        ChoicePageVO choicePageVO = new ChoicePageVO();

        QueryWrapper queryWrapper = new QueryWrapper();
        if (searchQuestionForm.getTitle() != null) {
            queryWrapper.like("choice_title", searchQuestionForm.getTitle());
        }
        if (searchQuestionForm.getTag() != null) {
            queryWrapper.like("choice_tag", searchQuestionForm.getTag());
        }
        IPage<Choice> page = new Page<>(searchQuestionForm.getCurrent(), searchQuestionForm.getSize());
        page = choiceMapper.selectPage(page, queryWrapper);

        choicePageVO.setCurrent(searchQuestionForm.getCurrent());
        choicePageVO.setSize(searchQuestionForm.getSize());
        choicePageVO.setPages(page.getPages());
        choicePageVO.setTotal(page.getTotal());
        choicePageVO.setChoices(page.getRecords());
        return choicePageVO;
    }

    /**
     * 根据公司id查询题目
     *
     * @param companyId
     * @return
     * */
    @Override
    public List<Choice> findByCompanyId(String companyId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("choice_company_id", companyId);
        return choiceMapper.selectList(queryWrapper);
    }

    /**
     * 根据题目id查询题目
     *
     * @param id
     * @return
     * */
    @Override
    public Choice findOneById(String id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("choice_id", id);
        return choiceMapper.selectOne(queryWrapper);
    }

    /**
     * 根据id列表查询题目列表
     *
     * @param ids
     * @return
     * */
    @Override
    public List<Choice> findListByIds(List<String> ids) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in("choice_id", ids);
        return choiceMapper.selectList(queryWrapper);
    }

    /**
     * 根据id列表获取多道题目答案
     *
     * @param ids
     * @return
     * */
    @Override
    public List<String> findKeyByIds(List<String> ids) {
        List<String> keyList = new ArrayList<>();

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select("choice_key").in("choice_id", ids);
        if (ids.size() != 0) {
            List<Choice> choiceList = choiceMapper.selectList(queryWrapper);
            for (Choice choice : choiceList) {
                keyList.add(choice.getChoiceKey());
            }
            return keyList;
        }
        return null;
    }

    /**
     * 根据id列表获取多道题目分数
     *
     * @param ids
     * @return
     * */
    @Override
    public List<Integer> findGradeByIds(List<String> ids) {
        List<Integer> gradeList = new ArrayList<>();

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select("choice_grade").in("choice_id", ids);
        if (ids.size() != 0) {
            List<Choice> choiceList = choiceMapper.selectList(queryWrapper);
            for (Choice choice : choiceList) {
                gradeList.add(choice.getChoiceGrade());
            }
            return gradeList;
        }
        return null;
    }
}
